package com.micro.user.repository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.micro.user.entity.Passenger;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.util.List;

/**
 * 乘客数据访问接口
 * 
 * @author micro
 * @since 1.0.0
 */
@Mapper
public interface PassengerRepository extends BaseMapper<Passenger> {

    /**
     * 根据用户ID查找乘客列表
     */
    @Select("SELECT * FROM passenger_info WHERE user_id = #{userId} AND active = true")
    List<Passenger> findByUserId(@Param("userId") Long userId);

    /**
     * 根据身份证号查找乘客
     */
    @Select("SELECT * FROM passenger_info WHERE id_card_number = #{idCardNumber} AND active = true")
    Passenger findByIdCardNumber(@Param("idCardNumber") String idCardNumber);

    /**
     * 根据乘客类型查找乘客列表
     */
    @Select("SELECT * FROM passenger_info WHERE passenger_type = #{passengerType} AND active = true")
    List<Passenger> findByPassengerType(@Param("passengerType") Integer passengerType);

    /**
     * 根据性别查找乘客列表
     */
    @Select("SELECT * FROM passenger_info WHERE gender = #{gender} AND active = true")
    List<Passenger> findByGender(@Param("gender") Integer gender);

    /**
     * 根据年龄范围查找乘客列表
     */
    @Select("SELECT * FROM passenger_info WHERE birth_date <= #{maxBirthDate} AND birth_date >= #{minBirthDate} AND active = true")
    List<Passenger> findByAgeRange(@Param("minBirthDate") LocalDate minBirthDate, @Param("maxBirthDate") LocalDate maxBirthDate);

    /**
     * 查找所有活跃乘客
     */
    @Select("SELECT * FROM passenger_info WHERE active = true")
    List<Passenger> findAllActivePassengers();

    /**
     * 统计指定用户的乘客数量
     */
    @Select("SELECT COUNT(*) FROM passenger_info WHERE user_id = #{userId} AND active = true")
    Long countByUserId(@Param("userId") Long userId);

    /**
     * 统计指定乘客类型的乘客数量
     */
    @Select("SELECT COUNT(*) FROM passenger_info WHERE passenger_type = #{passengerType} AND active = true")
    Long countByPassengerType(@Param("passengerType") Integer passengerType);

    /**
     * 根据用户ID和乘客姓名查找乘客
     */
    @Select("SELECT * FROM passenger_info WHERE user_id = #{userId} AND passenger_name = #{passengerName} AND active = true")
    Passenger findByUserIdAndName(@Param("userId") Long userId, @Param("passengerName") String passengerName);
}
